package com.ict.ms.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ict.ms.common.StateMsg;
import com.ict.ms.port.facade.api.IPortInfoSearchService;
import com.ict.ms.port.facade.model.PortinfoSearch;

import javax.annotation.Resource;

/**
 * Created by FCC on 2017/2/21.
 * 港口信息查询
 */

@Controller
@RequestMapping("/portinfo")
public class PortInfoSearchController {
    @Resource
    IPortInfoSearchService portInfoSearchService;

    /**
     * 通过港口ID获取港口信息
     *
     * @Param map
     * @Param id
     * @Param latitude
     * @Param longitude
     */
    @ResponseBody
    @RequestMapping(value = "portinformation.do", method = RequestMethod.POST)
    public String getPortInformation(ModelMap map, @RequestParam(value = "id", required = false) int id) {
        String msg;
        String longitude;
        String latitude;
        String jsonStr;
        PortinfoSearch portinfoSearch = null;
        portinfoSearch = portInfoSearchService.getPortInfoById(id);
        if (portinfoSearch != null) {
            latitude = portinfoSearch.getLatitude_degrees() + "." + portinfoSearch.getLatitude_minutes();//获取伟度
            longitude = portinfoSearch.getLongitude_degrees() + "." + portinfoSearch.getLongitude_minutes();//获取伟度
            long la = getlong(latitude, portinfoSearch.getLatitude_hemisphere());
            long lo = getlong(longitude, portinfoSearch.getLongitude_hemisphere());
            portinfoSearch.setLatitude(la);
            portinfoSearch.setLongitude(lo);
            map.addAttribute("portObject", portinfoSearch);
            jsonStr = StateMsg.toJson(1, map);
            return jsonStr;
        }
        msg = "没有找到该港口信息";
        map.addAttribute("error", msg);
        jsonStr = StateMsg.toJson(-1, map);
        return jsonStr;
    }

    //拼接成 N 00 W 00 经纬度
    private String getLaAndLo(PortinfoSearch portinfoSearch, String laloStr) {
        if (laloStr.equals("la")) {
            laloStr = portinfoSearch.getLatitude_hemisphere() + " " + portinfoSearch.getLatitude_degrees() + "°" + portinfoSearch.getLatitude_minutes() + "′";
        } else {
            laloStr = portinfoSearch.getLongitude_hemisphere() + " " + portinfoSearch.getLongitude_degrees() + "°" + portinfoSearch.getLongitude_minutes() + "′";
        }
        return laloStr;
    }

    //乘以经纬度
    private long getlong(String lola, String lolabq) {
        if (lolabq.equals("W") | lolabq.equals("S")) {
            Double d = Double.parseDouble(lola);
            long aoll = (long) ((d * 600000) * -1);
            return aoll;
        }
        Double d = Double.parseDouble(lola);
        long aoll = (long) (d * 600000);
        return aoll;
    }
}
